/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import Utils.ValidateFocusLost;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import samoloty.model.IDataModel;
import samoloty.model.models.ConfigurationModel;
import samoloty.model.models.CrewModel;
import samoloty.model.models.PantryModel;
import samoloty.view.DowPanel;
import samoloty.view.InfoSubPanel;
import samoloty.view.MainWindow;
import samoloty.view.MyButton;

/**
 *
 * @author borysfan
 */
public class DowPanelControler implements MyControler {

    private DowPanel panel;
    private Controler controler;
    private IDataModel dataModel = null;
    private int acRegNum = 0;

    public DowPanelControler(Controler c) {
        controler = c;
        initPanel();
    }

    public DowPanelControler(Controler c, IDataModel data) {
        controler = c;
        dataModel = data;
        initPanel();
    }

    public void initPanel() {
        panel = new DowPanel();
        panel.setBwTextFocusListener(new ChangeBwText());
        panel.setBiTextFocusListener(new ChangeBwText());
        panel.setPantryFwdTextFocusListener(new ChangePantryFwdText());
        panel.setPantryMidTextFocusListener(new ChangePantryMidText());
        panel.setPantryAftTextFocusListener(new ChangePantryAftText());
        panel.setAdjTextFocusListener(new ChangeAdj());
        panel.setCrewComboActionListener(new ChangeCrewCombo());
        panel.setAcregKeyListener(new KeyAcregText());
        panel.setMtowTextFocList(new ChangeMtow());
        panel.setMlawTextFocList(new ChangeMlaw());
        panel.setConfigListener(new ChangeConfig());
        initData();
    }

    private void hideUnusedPantry() {
        if (dataModel != null) {
            PantryModel pantry = dataModel.getPantry();
            double aftIndex = pantry.getAftIndex();
            int aftMax = pantry.getAftMax();
            double fwdIndex = pantry.getFwdIndex();
            int fwdMax = pantry.getFwdMax();
            double midIndex = pantry.getMidIndex();
            double midMax = pantry.getMidMax();
            if (aftIndex == 0 && aftMax == 0) {//3 205,175,145
                controler.getDowPanelControler().getPanel().hidePantryAft();
            }
            if (midIndex == 0 && midMax == 0) {//2
                controler.getDowPanelControler().getPanel().hidePantryMid();
                shiftPantry("aft", 175);
            }
            if (fwdIndex == 0 && fwdMax == 0) {//1
                controler.getDowPanelControler().getPanel().hidePantryFwd();
                shiftPantry("mid", 145);
                shiftPantry("aft", 175);
            }
            if ((fwdIndex == 0 && fwdMax == 0) && (midIndex == 0 && midMax == 0)) {
                shiftPantry("aft", 145);
            }
        }
    }

    private void shiftPantry(String pan, int y) {
        controler.getDowPanelControler().getPanel().shiftPantry(pan, y);
    }

    public void setAcRegNum(int n) {
        acRegNum = n;
    }

    public void firstFocused() {
        panel.setBwTextFocused();
    }

    private class ChangeBwText implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();
        }
    }

    private class ChangePantryFwdText implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();
        }
    }

    private class ChangeConfig implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            controler.getPaxPanelControler().updateMax();
        }
    }

    private class ChangePantryMidText implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();
        }
    }

    private class ChangePantryAftText implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
            panel.setPlotPanel(controler.getPlotPanel());
        }
    }

    private class ChangeMtow implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            int vmtow = dataModel.getMaxWeights().getTakeOff();
            int c_vmtow = Integer.parseInt(panel.getMtow());
            if (c_vmtow < vmtow) {
                panel.setMtow(Integer.toString(c_vmtow));
                controler.getInfoPanelControler().set_vmtow((double) c_vmtow);

            } else {
                controler.getInfoPanelControler().set_vmtow(vmtow);
                panel.setMtow(Integer.toString(vmtow));
            }
            panel.setPlotPanel(controler.getPlotPanel());
            controler.getPlotPanel().update();
        }
    }

    private class ChangeMlaw implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            int vmlaw = dataModel.getMaxWeights().getLanding();
            int c_vmlaw = Integer.parseInt(panel.getMlaw());
            if (c_vmlaw < vmlaw) {
                panel.setMlaw(Integer.toString(c_vmlaw));
                controler.getInfoPanelControler().set_vmlaw((double) c_vmlaw);
            } else {
                panel.setMlaw(Integer.toString(vmlaw));
                controler.getInfoPanelControler().set_vmlaw(vmlaw);
            }
            panel.setPlotPanel(controler.getPlotPanel());
            controler.getPlotPanel().update();
        }
    }

    private class ChangeAdj implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            controler.getInfoPanelControler().update();
            controler.getPlotPanel().update();
        }
    }

    private class ChangeCrewCombo implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            controler.getInfoPanelControler().update();
            panel.setPlotPanel(controler.getPlotPanel());
        }
    }

    private class KeyAcregText implements KeyListener {

        public void keyTyped(KeyEvent ke) {
        }

        public void keyPressed(KeyEvent ke) {
            /*            String acReg = dataModel.getAcReg().getAcReg();//string wejściowy
            controler.getDowPanelControler().getPanel().setAcRegText((acReg + ke.getKeyChar()));*/
        }

        public void keyReleased(KeyEvent ke) {
        }
    }

    public DowPanel getPanel() {
        return panel;
    }

    public void setUpWindow() {
        MainWindow window = controler.getView();
        panel.setPlotPanel(controler.getPlotPanel());

        InfoPanelControler infoPanelControler = controler.getInfoPanelControler();
        InfoSubPanel infoSubPanel = infoPanelControler.getPanel();
        panel.addInfoSubPanel(infoSubPanel);

        window.setLeftPanelVisible(true);
        window.setRightPanelVisible(true);
        window.setAllBtnStyle(MyButton.ACTIVE_BUTTON);
        window.setDowBtnStyle(MyButton.CHOOSE_BUTTON);
        panel.setFirstFieldActive();
        controler.getPlotPanelControler().update();
        panel.setBiTextFocusListener(new ValidateFocusLost());
        panel.setBwTextFocusListener(new ValidateFocusLost());
    }

    public void reset() {
        panel.reset();
        panel.setBwTextFocused();
    }

    public void setDataModel(IDataModel dataModel) {
        this.dataModel = dataModel;
        initData();
    }

    private void initData() {
        if (dataModel != null) {
            panel.setAcRegText(dataModel.getAcReg().get(acRegNum));
            for (int i = 0; i < dataModel.getCrew().size(); i++) {
                panel.addCrewComboBoxItem(dataModel.getCrew().get(i).getDisplay());
            }
//            panel.setAcRegText(dataModel.getAcReg().getAcReg());

            for (int i = 0; i < dataModel.getConfiguration().size(); i++) {
                panel.addConfigComboBoxItem(dataModel.getConfiguration().get(i).getConfiguration());
            }

            int vmtow = dataModel.getMaxWeights().getTakeOff();
            int vmlaw = dataModel.getMaxWeights().getLanding();
            panel.setMtow(Integer.toString(vmtow));
            panel.setMlaw(Integer.toString(vmlaw));

            hideUnusedPantry();

        }
    }

    /*gettery - po get'ie następuje nazwa zmiennej taka jaka została określona w SCHEMAT.XLS
     *
     */
    public String get_vbasicweight() {
        String result = panel.get_vbasicweight();
        if (result.equals("")) {
            return "0";
        } else {
            return result;
        }
    }

    public String get_vbasicindex() {
        String result = panel.get_vbasicindex();
        if (result.equals("")) {
            return "0";
        } else {
            return result;
        }
    }

    public CrewModel get_vcrew() {
        String selectedCrew = panel.get_vcrew();
        ArrayList<CrewModel> crews = dataModel.getCrew();
        for (int i = 0; i < crews.size(); i++) {
            if (crews.get(i).getDisplay().equals(selectedCrew)) {
                return crews.get(i);
            }
        }
        return null;
    }

    public String get_vpantryfwd() {
        String result = panel.get_vpantryfwd();
        if (result.equals("")) {
            return "0";
        } else {
            return result;
        }
    }

    public String get_vpantrymid() {
        String result = panel.get_vpantrymid();
        if (result.equals("")) {
            return "0";
        } else {
            return result;
        }
    }

    public String get_vpantryaft() {
        String result = panel.get_vpantryaft();
        if (result.equals("")) {
            return "0";
        } else {
            return result;
        }
    }

    public ConfigurationModel get_vconfig() {
        String configName = panel.get_vconfig();
        ArrayList<ConfigurationModel> configs = dataModel.getConfiguration();
        for (int i = 0; i < configs.size(); i++) {
            if (configs.get(i).getConfiguration().equals(configName)) {
                return configs.get(i);
            }
        }
        return null;
    }

    public String geTvadj1weight() {
        return panel.geTvadj1weight();
    }

    public String geTvadj1index() {
        return panel.geTvadj1index();
    }

    public String geTvadj1description() {
        return panel.geTvadj1description();
    }

    public String geTvadj2weight() {
        return panel.geTvadj2weight();
    }

    public String geTvadj2index() {
        return panel.geTvadj2index();
    }

    public String geTvadj2description() {
        return panel.geTvadj2description();
    }

    public String geTvadj3weight() {
        return panel.geTvadj3weight();
    }

    public String geTvadj3index() {
        return panel.geTvadj3index();
    }

    public String geTvadj3description() {
        return panel.geTvadj3description();
    }

    public void setAcRegText(String text) {
        panel.setAcRegText(text);
    }

    public String get_vmlaw(){
        return panel.getMlaw();
    }

    public String get_vmtow(){
        return panel.getMtow();
    }

    public void set_vmlaw(String v){
        panel.setMlaw(v);
    }

    public void set_vmtow(String v){
        panel.setMtow(v);
    }

    
}
